package cn.dengta.webapp.trade.dto;

import java.io.Serializable;

import cn.dengta.common.model.Status;
import cn.dengta.webapp.trade.model.TradeType;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;
import me.codeplayer.util.EasyDate;

@Getter
@Setter
@Accessors(chain = true)
public class IntegralDeductionConfigDTO implements Serializable {

	public static final String KEY = "msg_timeout_config";

	/** 搭讪收益限制 0-关闭 1-启用 */
	private Integer greetEnabled;
	/** 搭讪收益方多少分钟内无回复收益减半 */
	private Integer greetHalvingTime;
	/** 搭讪收益方多少分钟内无回复则无收益，并退回金币 */
	private Integer greetNoTime;
	/** 私信收益限制 0-关闭 1-启用 */
	private Integer msgEnabled;
	/** 私信收益方多少分钟内无回复收益减半 */
	private Integer msgHalvingTime;
	/** 私信收益方多少分钟内无回复则无收益，并退回金币 */
	private Integer msgNoTime;

	public static IntegralDeductionConfigDTO init() {
		IntegralDeductionConfigDTO config = new IntegralDeductionConfigDTO();
		config.setGreetEnabled(0);
		config.setGreetHalvingTime(5);
		config.setGreetNoTime(10);
		config.setMsgEnabled(0);
		config.setMsgHalvingTime(10);
		config.setMsgNoTime(20);
		return config;
	}

	public boolean enabled(TradeType tradeType) {
		switch (tradeType) {
			case COIN_FOR_GREET:
				return Status.asResult(greetEnabled);
			case COIN_FOR_MSG:
				return Status.asResult(msgEnabled);
			default:
				return false;
		}
	}

	public int fullLifetime(TradeType tradeType) {
		switch (tradeType) {
			case COIN_FOR_GREET:
				return Status.asResult(greetEnabled) ? greetNoTime : -1;
			case COIN_FOR_MSG:
				return Status.asResult(msgEnabled) ? msgNoTime : -1;
			default:
				return -1;
		}
	}

	public Integer halfLifetime(TradeType tradeType) {
		switch (tradeType) {
			case COIN_FOR_GREET:
				return greetHalvingTime;
			case COIN_FOR_MSG:
				return msgHalvingTime;
			default:
				return null;
		}
	}

	public static int calcFactor(long sendTime, long now, int fullLifetime, int halfLifetime) {
		if (sendTime + halfLifetime * EasyDate.MILLIS_OF_MINUTE >= now) {
			return 2; // 全部收益
		} else if (sendTime + fullLifetime * EasyDate.MILLIS_OF_MINUTE >= now) {
			return 1;
		} else {
			return 0;
		}
	}

}
